package com.yklab.library.encrypt;

import com.yklab.library.json.JSONUtil;
import com.yklab.library.log.LogUtil;
import com.yklab.library.util.StringUtils;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESUtils {
    public static final String sKey = "ADD41B0E7B04457B";
    private static final String ENERYPT = "enerypt";

    public static Map<String, Object> Encrypt(Map<String, Object> params) {
        Map<String, Object> encryptParam = null;
        if (params != null && params.size() > 0) {
            LogUtil.i("请求参数", JSONUtil.toJSON(params));
            String strParam = Encrypt(JSONUtil.toJSON(params));
            encryptParam = new HashMap<>();
            encryptParam.put(ENERYPT, strParam);
        }
        return encryptParam;
    }


    /**
     * describe:加密
     * author:zhangcong
     * date:2017/5/27 14:53
     */
    public static String Encrypt(String sSrc) {
        try {
            return AESUtils.Encrypt(sSrc, AESUtils.sKey);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * describe:解密
     * author:zhangcong
     * date:2017/5/27 14:53
     */
    public static String Decrypt(String sSrc) {
        try {
            if (StringUtils.isEmpty(sSrc)) {
                return null;
            }
            return AESUtils.Decrypt(sSrc, AESUtils.sKey);
        } catch (Exception e) {
            return null;
        }
    }

    // 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (StringUtils.isEmpty(sSrc)) {
            return null;
        }
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
//        Security.addProvider(new BouncyCastleProvider());
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return new String(Base64.encodeBase64(encrypted), "utf-8");
    }

    //解密
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            sSrc = correctStr(sSrc);
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
//            Security.addProvider(new BouncyCastleProvider());
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.decodeBase64(sSrc.getBytes("utf-8"));
            try {
                System.out.printf("The length of the buffer to be decrypted is %d bytes|", encrypted1.length);
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "utf-8");
                return originalString;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static String correctStr(String sSrc) {
        try {
            byte[] encrypted1 = null;
            do {
                sSrc += "=";
                encrypted1 = Base64.decodeBase64(sSrc.getBytes("utf-8"));
            } while (encrypted1.length % 16 != 0);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return sSrc;
    }

//    public static void main(String[] args) {
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("username", "admin");
//        params.put("password", "123");
//        String ecrypt = Encrypt(JSON.toJSONString(params));
//        System.out.println("加密串:" + ecrypt);
//        String decrypt = Decrypt(ecrypt);
//        System.out.println("解密串:" + decrypt);
    //解密串:{"password":"123","username":"admin"}
//    }
}